<template>
  <div class="park-enterprise-page">
    <div class="content-wrapper">
      <!-- 页面头部 -->
      <div class="page-header">
        <div class="header-content">
          <div class="header-title">
            <h1>昌欣园区企业专区</h1>
            <p>汇聚优质企业资源，打造一体化供应链生态系统</p>
          </div>
          <!-- <div class="search-bar">
          <el-input
            v-model="searchForm.keyword"
            placeholder="搜索企业名称、类型或业务..."
            clearable
            @keyup.enter="handleSearch"
            class="search-input"
          />
          <div class="search-info">
            <span>当前展示{{ total }}个企业</span>
            <el-button type="primary" @click="handleSearch" class="search-btn">搜索</el-button>
          </div>
        </div> -->
        </div>
      </div>
    </div>

    <!-- 企业列表 -->
    <div class="list-section">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="5" animated />
      </div>

      <!-- 企业列表 -->
      <div v-else-if="enterpriseList.length > 0" class="enterprises-grid">
        <div
          v-for="(enterprise, index) in enterpriseList"
          :key="enterprise.id"
          class="enterprise-card"
          :class="`theme-${getTypeTheme(enterprise.type)}`"
        >
          <div class="enterprise-image">
            <img :src="enterprise.imageUrl" class="enterprise-img" />
          </div>
          <!-- {{ enterprise.imageUrl }} -->
          <div class="enterprise-info">
            <div class="enterprise-name">{{ enterprise.name }}</div>
            <div class="enterprise-type">{{ getCompanyTypeLabel(enterprise.type) }}</div>
            <div class="enterprise-desc">{{ enterprise.description }}</div>
            <div class="enterprise-actions">
              <el-button type="primary" class="supply-chain-btn" @click="handleSupplyChainLogin">
                供应链平台登录
              </el-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <el-empty description="暂无园区企业数据" />
      </div>

      <!-- 分页 -->
      <div v-if="total > 0" class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { useParkEnterprise } from '~/composables/useParkEnterprise'
import { ElMessage, ElMessageBox } from 'element-plus'
import { linkJump } from '~/utils/util'

// 使用园区企业组合式函数
const {
  loading,
  enterpriseList,
  total,
  currentPage,
  pageSize,
  fetchEnterpriseList,
  handlePageChange,
} = useParkEnterprise()

// 搜索表单
const searchForm = reactive({
  keyword: '',
})

// 处理供应链平台登录
const handleSupplyChainLogin = async () => {
  try {
    await ElMessageBox.confirm('确认跳转到供应链平台登录页面吗？', '供应链平台登录', {
      confirmButtonText: '前往登录',
      cancelButtonText: '取消',
      type: 'info',
      confirmButtonClass: 'custom-confirm-btn',
      cancelButtonClass: 'custom-cancel-btn',
      customClass: 'custom-supply-chain-dialog',
      showClose: false,
      center: true,
    })
    linkJump('http://192.168.1.138/')
  } catch (error) {
    // 用户取消操作，不执行任何操作
  }
}

// 图片URL处理函数
const getImageUrl = (imagePath: string | undefined | null) => {
  if (!imagePath || typeof imagePath !== 'string') return ''
  // 如果已经是完整URL，直接返回
  if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
    return imagePath
  }
  // 从环境变量获取API基础URL
  const config = useRuntimeConfig()
  const apiUrl = config.public.apiBase || 'http://192.168.1.141:8081'
  return `${apiUrl}${imagePath}`
}

// 根据公司类型设置颜色主题：1=蓝色(全资)、2=绿色(控股)、3=紫色(参股)
const getTypeTheme = (type: number | string | undefined) => {
  const t = Number(type)
  if (t === 1) return 'blue'
  if (t === 2) return 'green'
  if (t === 3) return 'purple'
  return 'blue'
}

// 公司类型枚举映射：1=全资子公司, 2=控股公司, 3=参股公司
const getCompanyTypeLabel = (type: number | string | undefined) => {
  const t = Number(type)
  if (t === 1) return '全资子公司'
  if (t === 2) return '控股公司'
  if (t === 3) return '参股公司'
  return '未知类型'
}

// 搜索处理
const handleSearch = async () => {
  currentPage.value = 1
  await loadEnterpriseList()
}

// 加载园区企业列表
const loadEnterpriseList = async () => {
  try {
    await fetchEnterpriseList({
      ...searchForm,
      page: currentPage.value,
      limit: pageSize.value,
    })
  } catch (error) {
    // ElMessage.error('加载园区企业列表失败')
  }
}

// 分页处理
const handleSizeChange = (val: number) => {
  pageSize.value = val
  currentPage.value = 1
  loadEnterpriseList()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  loadEnterpriseList()
}

// 页面加载时获取数据
onMounted(() => {
  loadEnterpriseList()
})
</script>

<style scoped lang="scss">
.park-enterprise-page {
  background-color: #ffffff;
  padding: 40px 0;
  margin: 20px auto !important;
  min-height: 100vh;
  max-width: 1400px !important;
  width: 100% !important;
  box-sizing: border-box;

  .content-wrapper {
    max-width: 1400px !important;
    margin: 0 auto !important;
    padding: 0 20px;
    width: 100% !important;
    box-sizing: border-box;
  }
}

.page-header {
  background: #fff;
  padding: 20px 0;
  margin-bottom: 15px;
  border-bottom: 1px solid #e4e7ed;

  .header-content {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
  }

  .header-title {
    text-align: center;
    margin-bottom: 15px;

    h1 {
      margin: 0 0 8px 0;
      color: #333;
      font-size: 36px;
      font-weight: 700;
      letter-spacing: 1px;
    }

    p {
      margin: 0;
      color: #666;
      font-size: 16px;
      line-height: 1.6;
    }
  }

  .search-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    max-width: 800px;
    margin: 0 auto;

    .search-input {
      flex: 1;
      max-width: 500px;

      :deep(.el-input__wrapper) {
        border-radius: 25px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        border: 1px solid #e4e7ed;
        padding: 12px 20px;
        font-size: 14px;

        &:hover {
          border-color: #d9d9d9;
        }

        &.is-focus {
          border-color: #d9d9d9;
          box-shadow: 0 0 0 2px rgba(217, 217, 217, 0.2);
        }
      }
    }

    .search-info {
      display: flex;
      align-items: center;
      gap: 15px;

      span {
        color: #666;
        font-size: 14px;
        white-space: nowrap;
      }

      .search-btn {
        border-radius: 25px;
        padding: 12px 30px;
        font-weight: 500;
        font-size: 14px;
        box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
        border: none;

        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
        }
      }
    }
  }
}

.list-section {
  .loading-container {
    padding: 40px;
  }

  .enterprises-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 30px;
    margin-bottom: 30px;

    .enterprise-card {
      background: white;
      border-radius: 12px;
      overflow: hidden;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);

        .enterprise-img {
          transform: scale(1.05);
        }
      }

      .enterprise-image {
        width: 100%;
        height: 200px;
        overflow: hidden;
        background: #f0f0f0;
        display: flex;
        align-items: center;
        justify-content: center;

        .enterprise-img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transition: transform 0.3s ease;
        }

        .image-placeholder {
          color: #999;
          font-size: 16px;
          font-weight: 500;
        }
      }

      .enterprise-info {
        padding: 20px;

        .enterprise-name {
          font-size: 18px;
          font-weight: 600;
          color: #333;
          margin-bottom: 8px;
          line-height: 1.3;
          text-align: center;
        }

        .enterprise-type {
          display: block;
          padding: 4px 10px;
          border-radius: 999px;
          font-size: 14px;
          font-weight: 500;
          margin: 8px auto 12px;
          border: 1px solid transparent;
          width: fit-content;
          text-align: center;
        }

        .enterprise-desc {
          font-size: 14px;
          color: #666;
          line-height: 1.6;
          margin-bottom: 20px;
          height: 105px;
          overflow: hidden;
          display: -webkit-box;
          -webkit-line-clamp: 5;
          line-clamp: 5;
          -webkit-box-orient: vertical;
        }

        .supply-chain-btn {
          width: 100%;
          background: white;
          border: 2px solid #52c41a;
          border-radius: 8px;
          height: 42px;
          font-size: 14px;
          font-weight: 500;
          color: #52c41a;
          cursor: pointer;
          transition: all 0.3s ease;

          &:hover {
            background: #52c41a;
            color: white;
            transform: translateY(-1px);
          }

          &:active {
            transform: translateY(0);
          }
        }
      }
    }
  }

  .empty-state {
    text-align: center;
    padding: 40px;
  }

  .pagination {
    margin-top: 20px;
    text-align: center;
  }
}

// 不同颜色主题样式
.theme-blue {
  .enterprise-type {
    color: #1890ff !important;
    border-color: #1890ff !important;
    background: rgba(24, 144, 255, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #1890ff !important;
    color: #1890ff !important;
    &:hover {
      background: #1890ff !important;
      color: white !important;
    }
  }
}

.theme-red {
  .enterprise-type {
    color: #ff4d4f !important;
    border-color: #ff4d4f !important;
    background: rgba(255, 77, 79, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #ff4d4f !important;
    color: #ff4d4f !important;
    &:hover {
      background: #ff4d4f !important;
      color: white !important;
    }
  }
}

.theme-green {
  .enterprise-type {
    color: #52c41a !important;
    border-color: #52c41a !important;
    background: rgba(82, 196, 26, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #52c41a !important;
    color: #52c41a !important;
    &:hover {
      background: #52c41a !important;
      color: white !important;
    }
  }
}

.theme-yellow {
  .enterprise-type {
    color: #faad14 !important;
    border-color: #faad14 !important;
    background: rgba(250, 173, 20, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #faad14 !important;
    color: #faad14 !important;
    &:hover {
      background: #faad14 !important;
      color: white !important;
    }
  }
}

.theme-purple {
  .enterprise-type {
    color: #722ed1 !important;
    border-color: #722ed1 !important;
    background: rgba(114, 46, 209, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #722ed1 !important;
    color: #722ed1 !important;
    &:hover {
      background: #722ed1 !important;
      color: white !important;
    }
  }
}

.theme-cyan {
  .enterprise-type {
    color: #13c2c2 !important;
    border-color: #13c2c2 !important;
    background: rgba(19, 194, 194, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #13c2c2 !important;
    color: #13c2c2 !important;
    &:hover {
      background: #13c2c2 !important;
      color: white !important;
    }
  }
}

.theme-orange {
  .enterprise-type {
    color: #fa8c16 !important;
    border-color: #fa8c16 !important;
    background: rgba(250, 140, 22, 0.08) !important;
  }
  .supply-chain-btn {
    border-color: #fa8c16 !important;
    color: #fa8c16 !important;
    &:hover {
      background: #fa8c16 !important;
      color: white !important;
    }
  }
}

// 响应式设计
@media (max-width: 1400px) {
  .enterprises-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 20px;
  }
}

// 自定义供应链平台登录弹窗样式
:deep(.custom-supply-chain-dialog) {
  border-radius: 12px !important;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15) !important;
  border: none !important;
  overflow: hidden;

  .el-message-box__header {
    background-color: #f5f7fa !important;
    padding: 16px 24px !important;
    border-bottom: 1px solid #ebeef5;

    .el-message-box__title {
      font-size: 16px !important;
      font-weight: 600 !important;
      color: #303133 !important;
    }
  }

  .el-message-box__content {
    padding: 24px !important;
    font-size: 14px !important;
    color: #606266 !important;
    line-height: 1.6;
  }

  .el-message-box__btns {
    padding: 12px 24px !important;
    background-color: #fafafa !important;
    border-top: 1px solid #ebeef5;
    display: flex;
    justify-content: center;
    gap: 12px;
  }
}

:deep(.custom-confirm-btn) {
  background-color: #1890ff !important;
  border-color: #1890ff !important;
  color: white !important;
  padding: 8px 16px !important;
  border-radius: 6px !important;
  font-size: 14px !important;
  font-weight: 500 !important;
  transition: all 0.3s ease;

  &:hover,
  &:focus {
    background-color: #40a9ff !important;
    border-color: #40a9ff !important;
    color: white !important;
  }
}

:deep(.custom-cancel-btn) {
  background-color: white !important;
  border-color: #dcdfe6 !important;
  color: #606266 !important;
  padding: 8px 16px !important;
  border-radius: 6px !important;
  font-size: 14px !important;
  transition: all 0.3s ease;

  &:hover,
  &:focus {
    border-color: #c0c4cc !important;
    color: #409eff !important;
  }
}

@media (max-width: 768px) {
  .park-enterprise-page {
    padding: 10px;
  }

  .page-header {
    .header-title h1 {
      font-size: 28px;
    }

    .search-bar {
      flex-direction: column;
      gap: 15px;

      .search-input {
        max-width: 100%;
      }
    }
  }

  .enterprises-grid {
    grid-template-columns: 1fr !important;
    gap: 15px;
  }
}
</style>
